/**
 * Created by Administrator on 2017/8/14.
 */

/*document.addEventListener("deviceready",onDeviceReady,false);

window.localStorage.removeItem("isBusLine");
window.localStorage.removeItem("isBusStation");
window.localStorage.removeItem("direction");
window.localStorage.removeItem("busgprsid");
window.localStorage.removeItem("busfullname");
window.localStorage.removeItem("stationorderno");
window.localStorage.removeItem("stationfullname");

//Cordova加载完成会触发
function onDeviceReady() {
    showAlert("deviceReady", 2000);
}

function getCurrentPosition(){
    //定位数据获取成功响应
    var onSuccess = function(position) {
        showAlert('纬度: '          + position.coords.latitude          + '\n' +
            '经度: '         + position.coords.longitude         + '\n' +
            '海拔: '          + position.coords.altitude          + '\n' +
            '水平精度: '          + position.coords.accuracy          + '\n' +
            '垂直精度: ' + position.coords.altitudeAccuracy  + '\n' +
            '方向: '           + position.coords.heading           + '\n' +
            '速度: '             + position.coords.speed             + '\n' +
            '时间戳: '         + position.timestamp                + '\n');
    };
    //定位数据获取失败响应
    function onError(error) {
        showAlert('code: '    + error.code    + '\n' +
            'message: ' + error.message + '\n');
    }

    //开始获取定位数据
    navigator.geolocation.getCurrentPosition(onSuccess, onError);
}*/

var map;
var autoConfirmValue;

$(document).on('pageshow','#homePage',function () {
    initMap();
    initControl();
    initAutoComplete();
    bindGetlocation();
});

//初始化地图
initMap = function(){
    var mapOpts = {minZoom:11,maxZoom:17,enableHighResolution:true};
    map = new BMap.Map('map',mapOpts);
    map.centerAndZoom("杭州");
};

//初始化地图控件
initControl = function(){
    var navOpts = {anchor:BMAP_ANCHOR_BOTTOM_RIGHT,offset:new BMap.Size(50,40),type:BMAP_NAVIGATION_CONTROL_ZOOM};
    var nav = new BMap.NavigationControl(navOpts);
    map.addControl(nav);

/*    var scale = new BMap.ScaleControl();
    map.addControl(scale);*/

    var overOpts = {anchor:BMAP_ANCHOR_TOP_LEFT,offset:new BMap.Size(10,10),size:new BMap.Size(100,100),isOpen:false};
    var over = new BMap.OverviewMapControl(overOpts);
    map.addControl(over);

    var mapTypeC = new BMap.MapTypeControl();
    map.addControl(mapTypeC);
};

//初始化自动完成
initAutoComplete = function(){
    var autoCompleteOpts = {
        location:"杭州",
        input:'positionSearch'
    };
    var autoC = new BMap.Autocomplete(autoCompleteOpts);

    var lovalSearchOpts = {
        renderOptions:{map: map}
    };
    var localSearch = new BMap.LocalSearch(map,lovalSearchOpts);

    autoC.addEventListener('onconfirm',function(e){
        var _value = e.item.value;
        autoConfirmValue = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
        localSearch.search(autoConfirmValue);
        $('#positionSearch').val("");
        //将光标定位在隐藏的div上 防止看不到搜索框
        $('#positionSearch').blur();
        $("#focusAnchor").focus();
    });

};

bindGetlocation = function(){
    $("#getlocation").click(function(){
        var geolocation = new BMap.Geolocation();
        geolocation.getCurrentPosition(function(r){
            if(this.getStatus() == BMAP_STATUS_SUCCESS){
                var mk = new BMap.Marker(r.point);
                map.addOverlay(mk);
                map.panTo(r.point);
            }else{
                showAlert("获取位置失败，错误码" + this.getStatus(),2000);
            }
        });
    });
};

function showAlert(msg, timeout) {
    var $popup;
    if ($("#showmsg").length == 0) {
        var showmsgdiv = '<div data-role="popup" id="showmsg" data-theme="b" class="ui-content">' +
            '<p id="showcontent" style="padding: 15px 15px 5px 5px;">' + msg + '</p></div>';
        $popup = $(showmsgdiv);
    } else {
        $popup = $("#showmsg");
        $("#showcontent").html(msg);
    }
    $popup.popup({history: false});
    $popup.popup();
    $popup.popup('open');
    if (!(typeof(timeout) == "number" && timeout > 0)) {
        timeout = 2000;
    }
    var msgshow = setTimeout(function () {
        $popup.popup('close');
    }, timeout);
}